package demo.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;
import java.util.Scanner;

public class Consumer {
    /**
     * 一、点对点模式
     * @throws JMSException
     * @throws IOException
     */
    public static void consume() throws JMSException, IOException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("myqueue");
        MessageConsumer consumer = session.createConsumer(queue);
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接收消息："+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        Scanner sc = new Scanner(System.in);
        System.out.println("input 'exit' to eixt:");
        while(true){
            String text = sc.next();
            if("exit".equals(text)) break;
        }
        consumer.close();
        session.close();
        connection.close();
    }

    public static void main(String[] args) throws IOException, JMSException {
        consume();
    }
}
